
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%>
<%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<%@page import="java.io.*"%>
<%@page import="org.jdom.input.SAXBuilder,org.jdom.Document"%>
<%@page import="org.jdom.Element"%>
<%@page import="org.w3c.dom.NodeList"%>
<%
	System.out.println("-----------------滚石接受报告,解析报告-------------------");
	String syncDataSourceIp = request.getRemoteHost();
	String recvMsgType = "";
	String saveGwid = "";
	response.setContentType("text/plain");
	PrintWriter out1 = response.getWriter();

	InputStream in = request.getInputStream();
	StringBuffer provisionReq = new StringBuffer(512);
	byte[] buf = new byte[1024];
	int len = 0;
	try {
		while ((len = in.read(buf)) != -1) {
			provisionReq.append(new String(buf, 0, len, "UTF-8"));
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
	in = new ByteArrayInputStream(provisionReq.toString().trim().getBytes("UTF-8"));
	SAXBuilder builder = new SAXBuilder();
	try {
		Document doc = builder.build(in);
		Element root = doc.getRootElement();
		String rootElementName = root.getName();
		
		//解析节点
		String feeType = root.getChild("DATA_TYPE").getValue();//CO_SMS_REPORT
		String linkId = root.getChild("TRANSACTIONID").getValue();
		//String spNo = root.getChild("SRC_TERMID").getValue();
		String userPhone = root.getChild("DST_TERMID").getValue();
		String momsg = root.getChild("CONTENT").getValue();           //下行内容  
		String srvCode = root.getChild("SERVICEID").getValue();
		String fee = root.getChild("FEE_CODE").getValue();//资费
		String status = root.getChild("STATUS").getValue();//成功为：DELIVRD；其他为错误码
		String date=root.getChild("SENDTIME").getValue();
		
		//--------调用业务方法，把报告信息插入表中（数据库：db_bak; 表：sp_status）--------
		//SpStatusService objService = new SpStatusService();
		//SpStatusBean objBean = new SpStatusBean();
		//SpReceiveBean objReceBean = new SpReceiveBean();
		//objReceBean = SpReceiveService.getInstance().findByPhoneAndLinkId(DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString()).substring(2,8),userPhone,linkId);
		
		//把解析后的数据保存下来
		//objBean.setCreateTime(DateUtil.getNow_Timestamp().toString());
		//objBean.setFee(fee);
		//if(Integer.parseInt(fee)==200||Integer.parseInt(fee)==100){
		//	objBean.setFeeType("2");
		//}
		//else{
		//	objBean.setFeeType("1");
		//}
		
		//objBean.setGatewayId("");
		//objBean.setLinkId(linkId);
		//objBean.setMid("");
		//objBean.setMsgId("");
		//objBean.setSpCode(spNo);
		//objBean.setSrvCode(srvCode);
		//objBean.setStatus(status);
		//objBean.setUserPhone(userPhone);
		//objBean.setNewMsgId("");
		//objBean.setPrdType(objReceBean.getPrdType());
		//objService.addSpStatusByDay(DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString()).substring(2,8),objBean);//保存到状态天表中
		String spNo = SqlUtil.getStringResult("select dest from recv where linkid = '"+linkId+"'",DbOperation.DB_STAT);
    	String content = SqlUtil.getStringResult("select body from recv where linkid = '"+linkId+"'",DbOperation.DB_STAT);
    
    	System.out.println("*********");
    	int provinceId = BillingUtil.getProvinceId(userPhone);
    	System.out.println(provinceId);
		StringBuilder sb = new StringBuilder();
	    sb.append("'");
	    sb.append(provinceId);
	    sb.append("',");
	    sb.append("1");//msgmode
	    sb.append(",");
	    sb.append("15");//fmt
	    sb.append(",");
	    sb.append("0");//futype
	    sb.append(",");
	    sb.append("2");//ftype
	    sb.append(",");
	    sb.append(fee);//feecode  
	    sb.append(",");
	    sb.append("'");
	    sb.append("XXLYYY");//stype
	    sb.append("',");
	    sb.append("1");//num
	    sb.append(",");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("'");
	    sb.append(spNo);
	    sb.append("',");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("getDate()");  //tm
	    sb.append(",");
	    sb.append("'");
	    sb.append("mid");  //mid
	    sb.append("',");
	    sb.append("'");
	    sb.append("msgid");  //msgid
	    sb.append("',");
	    sb.append("0");//result  DELIVRD
	    sb.append(",");
	    sb.append("'");
	    sb.append(status);//DELIVRD
	    sb.append("',");
	    sb.append("'");
	    sb.append("infoid");//infoid
	    sb.append("',");
	    sb.append("'");
	    sb.append(linkId);//linkid
	    sb.append("','"+content+"'");
	    sb.append(",");
	    sb.append(provinceId);
	        
	    SqlUtil.executeUpdate("insert into "+BillingUtil.getSendDay()+"(areaid,msgMode,fmt,futype,ftype,feecode,stype,num,org,dest,fee,tm,mid,msgid,result,stat,infoid,linkid,body,province) values("+sb.toString()+")",DbOperation.DB_STAT);
		
	    
	    if(content!=null&&content.equals("11")&&spNo!=null&&spNo.equals("106699679")) {
			StringBuilder mo = new StringBuilder("http://121.14.118.209:9527/sms/kongjian/kongjianMr.jsp?userPhone=");
			mo.append(userPhone);
			mo.append("&spNo=");
			mo.append(spNo);
			mo.append("&linkId=");
			mo.append(linkId);
			mo.append("&mo=");
			mo.append(content);
			mo.append("&date=");
			mo.append(date);
			mo.append("&fee=");
			mo.append(fee);
			mo.append("&status=");
			mo.append(status);
			System.out.println(mo.toString());
			System.out.println(HttpUtil.getHtmlSource(mo.toString(),"UTF-8"));
		}
		
		
		String xmlStr="<?xml version=\"1.0\" encoding=\"gb2312\" ?><MASP_RESP><RESULT>0</RESULT><RESULT_MSG>OK</RESULT_MSG></MASP_RESP>";
		out1.print(xmlStr);
		out1.flush();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			out1.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
%>
